package com.rao.crm.controller;

import com.rao.crm.pojo.BaseDict;
import com.rao.crm.pojo.Customer;
import com.rao.crm.pojo.QueryVo;
import com.rao.crm.service.BaseDictService;
import com.rao.crm.service.CustomerService;
import com.rao.crm.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className CustomerController
 * @date 2019/10/6 13:55
 * @package com.rao.crm.controller
 * @description
 */
@Controller
public class CustomerController {

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private CustomerService customerService;

    @Value("${CUSTOMER_FROM_TYPE}")
    private String CUSTOMER_FROM_TYPE;

    @Value("${CUSTOMER_INDUSTRY_TYPE}")
    private String CUSTOMER_INDUSTRY_TYPE;

    @Value("${CUSTOMER_LEVEL_TYPE}")
    private String CUSTOMER_LEVEL_TYPE;

    /*跳转到首页，并回显分类数据*/
    @RequestMapping("list")
    public String list(QueryVo queryVo, Model model){

        /*客户来源*/
        List<BaseDict> fromType = baseDictService.findBaseDictByTypeCode(CUSTOMER_FROM_TYPE);
        /*所属行业*/
        List<BaseDict> industryType = baseDictService.findBaseDictByTypeCode(CUSTOMER_INDUSTRY_TYPE);
        /*客户级别*/
        List<BaseDict> levelType = baseDictService.findBaseDictByTypeCode(CUSTOMER_LEVEL_TYPE);
        /*把分类信息加载到页面*/
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);

        /*查询当前页的信息*/
        Page<Customer> page = customerService.findCustomerByQueryVo(queryVo);
        model.addAttribute("page",page);
        /*分类信息回显*/
        model.addAttribute("custName", queryVo.getCustName());
        model.addAttribute("custSource", queryVo.getCustSource());
        model.addAttribute("custIndustry", queryVo.getCustIndustry());
        model.addAttribute("custLevel", queryVo.getCustLevel());


        return "customer";
    }


    /*编辑客户界面*/
    @RequestMapping("edit")
    public @ResponseBody
    Customer edit(Long id){
        /*System.out.println(id + "===========================")*/;
        return customerService.findCustomerById(id);
    }


    /*修改客户*/
    @RequestMapping("update")
    public @ResponseBody String update(Customer customer){
        if (customer.getCust_id() == null) {
            add(customer);
        }
        customerService.updateCustomerById(customer);
        return "OK";
    }

    /*删除客户*/
    @RequestMapping("delete")
    public @ResponseBody String delete(Long id){
        customerService.deleteCustomerById(id);
        return "OK";
    }

    /*新增客户*/
    @RequestMapping("add")
    public @ResponseBody String add(Customer customer){
        customerService.addCustomer(customer);
        return "OK";
    }
}
